package com.amigo.online.provider.search.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.core.security.IgnoreSecurity;
import com.amigo.online.common.param.dto.query.MerchantGoodsDto;
import com.amigo.online.common.param.dto.query.Searchparam;
import com.amigo.online.common.util.StringUtils;
import com.amigo.online.provider.search.aspect.EscapeQueryChars;
import com.amigo.online.provider.search.exception.SearchException;
import com.amigo.online.provider.search.service.SolrService;

@RestController
@RequestMapping(value = "/search", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SolrController extends BaseController {

    @Autowired
    SolrService solrService;

    @IgnoreSecurity
    @EscapeQueryChars
    @PostMapping(value = "/searchResult")
    public ResponseEntity<Response> search(@RequestBody Searchparam searchparam) throws Exception {
        if (searchparam == null) {
            throw new SearchException(StatusCode.BAD_REQUEST);
        }
        if (StringUtils.isBlank(searchparam.getQ())) {
            throw new SearchException(StatusCode.BAD_REQUEST);
        }
        if (StringUtils.isBlank(searchparam.getStart())) {
            throw new SearchException(StatusCode.BAD_REQUEST);
        }
        if (StringUtils.isBlank(searchparam.getRows())) {
            throw new SearchException(StatusCode.BAD_REQUEST);
        }

        return success(solrService.search(searchparam));
    }

    @IgnoreSecurity
    @GetMapping(value = "/getHotWord")
    public ResponseEntity<Response> getHotWord() {
        return success(solrService.getHotWord());
    }

    @IgnoreSecurity
    @PostMapping(value = "/merchant")
    public ResponseEntity<Response> merchantSearch(@RequestBody Searchparam searchparam) {
        return success(solrService.merchantSearch(searchparam));
    }

    @IgnoreSecurity
    @PostMapping(value = "/merchantGoods")
    public ResponseEntity<Response> merchantGoods(@RequestBody MerchantGoodsDto searchparam) {
        if (searchparam.getMerchantId() == null) {
            throw new SearchException(StatusCode.BAD_REQUEST);
        }
        return success(solrService.merchantGoodsSearch(searchparam));
    }

}
